package com.ourcraft.informationcollectionsystem.mapper;

import com.github.yulichang.base.MPJBaseMapper;
import com.ourcraft.informationcollectionsystem.entity.Company;
import com.ourcraft.informationcollectionsystem.entity.CompanyRecordDetail;
import com.ourcraft.informationcollectionsystem.entity.DTO.UserDetailInfo;
import com.ourcraft.informationcollectionsystem.entity.DataReportFormDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 查询主表为Company的数据
 * @author SceneryMC
 * @since 2023-10-31
 */
@Mapper
public interface CompanyMapper extends MPJBaseMapper<Company> {

    /**
     * 查询未注销的所有企业信息
     * @return 未注销的所有企业信息
     */
    @Select("SELECT c.company_id as companyId " +
            "FROM Company c " +
            "LEFT JOIN User u ON u.user_id = c.user_id " +
            "WHERE u.user_status = true AND c.company_record_status = 4 "+
            "ORDER BY c.company_id ASC")
    @Results({
            @Result(column = "companyId", property = "companyId")
    })
    List<Company> selectActiveCompanyIds();

    @Select("select c.company_id as companyId, c.company_city as companyCity, " +
            "c.company_location as companyLocation, c.company_inst_code as companyInstCode, " +
            "c.company_chinese_name as companyChineseName, c.company_english_name as companyEnglishName, " +
            "c.company_nature as companyNature, c.company_industry as companyIndustry, " +
            "c.company_business as companyBusiness, c.company_contacts as companyContacts, " +
            "c.company_address as companyAddress, c.company_postcode as companyPostcode, " +
            "c.company_phone as companyPhone, c.company_fax as companyFax, " +
            "c.company_email as companyEmail, c.company_record_status as companyRecordStatus, " +
            "h.history_date as lastSubmitDate " +
            "from Company c " +
            "left join (select h1.company_id as company_id, max(h1.history_date) as history_date from CompanyInfoHistory h1 where h1.history_type = 0 group by h1.company_id) h " +
            "on c.company_id = h.company_id;")
    @Results({
            @Result(column = "companyId", property = "companyId"),
            @Result(column = "companyCity", property = "companyCity"),
            @Result(column = "companyLocation", property = "companyLocation"),
            @Result(column = "companyInstCode", property = "companyInstCode"),
            @Result(column = "companyChineseName", property = "companyChineseName"),
            @Result(column = "companyEnglishName", property = "companyEnglishName"),
            @Result(column = "companyNature", property = "companyNature"),
            @Result(column = "companyIndustry", property = "companyIndustry"),
            @Result(column = "companyBusiness", property = "companyBusiness"),
            @Result(column = "companyContacts", property = "companyContacts"),
            @Result(column = "companyAddress", property = "companyAddress"),
            @Result(column = "companyPostcode", property = "companyPostcode"),
            @Result(column = "companyPhone", property = "companyPhone"),
            @Result(column = "companyFax", property = "companyFax"),
            @Result(column = "companyEmail", property = "companyEmail"),
            @Result(column = "companyRecordStatus", property = "companyRecordStatus"),
            @Result(column = "lastSubmitDate", property = "lastSubmitDate"),
    })
    List<CompanyRecordDetail> selectCompanyRecordDetails();
}
